﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace UpUp
{
    class StartScreen : GameScreen
    {
        Texture2D bg;
        Texture2D title;
        Button start;
        Button quit;
        List<Texture2D> sprites;
        EffectsManager frontClouds;
        EffectsManager backClouds;
        double cloudTimer = 0;
        double cloudInterval = 3000;
        Vector2[] mousePos = new Vector2[2] { new Vector2(450, 520), new Vector2(225, 635) };
        bool isOpt0 = true;

        // Sounds
        String SFX_MenuMove = "Audio\\SFX\\Menu_Select_Move";
        String SFX_MenuPick = "Audio\\SFX\\Menu_Select_Pick";

        public override void LoadContent()
        {
            bg = Engine.Load<Texture2D>("Images\\StartScreen\\background");
            title = Engine.Load<Texture2D>("Images\\StartScreen\\title");
            start = new Button(Engine.Load<Texture2D>("Images\\StartScreen\\novo"), new Vector2(120, 484));
            quit = new Button(Engine.Load<Texture2D>("Images\\StartScreen\\sair"), new Vector2(120, 598));
            frontClouds = new EffectsManager();
            backClouds = new EffectsManager();
            sprites = new List<Texture2D>();
            Audio.AddSFX(SFX_MenuMove);
            Audio.AddSFX(SFX_MenuPick);
            Audio.PlaySong("Audio\\BGM\\Intro");
            MediaPlayer.IsRepeating = true;
            Mouse.SetPosition((int)mousePos[0].X, (int)mousePos[0].Y);
            for (int i = 1; i < 5; ++i) sprites.Add(Engine.Load<Texture2D>("Images\\StartScreen\\nuvem_" + i));
            UpUp.isMouseVisible = true;
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.KeyPressed(Keys.M)) Audio.ToggleMute();
            if (start.Clicked || 
                ((Input.KeyPressed(Keys.Enter) || Input.KeyPressed(Keys.Space) || Input.ButtonPressed(Buttons.Start) || Input.ButtonPressed(Buttons.A)) && isOpt0))
            {
                Audio.PlaySFX(SFX_MenuPick);
                GameScreenManager.ChangeScreen(new PlayScreen());
                UpUp.isMouseVisible = false;
                Audio.PlaySong("Audio\\BGM\\Play");
            }
            if (quit.Clicked || ((Input.KeyPressed(Keys.Enter) || Input.KeyPressed(Keys.Space) || Input.ButtonPressed(Buttons.Start) || Input.ButtonPressed(Buttons.A)) && !isOpt0))
            {
                Audio.PlaySFX(SFX_MenuPick);
                Engine.game.Exit();
            }
            if (Input.KeyPressed(Keys.Down) || Input.KeyPressed(Keys.Up))
            {
                Audio.PlaySFX(SFX_MenuMove);
                isOpt0 = !isOpt0;
                int temp = isOpt0 ? 0 : 1;
                Mouse.SetPosition((int)mousePos[temp].X, (int)mousePos[temp].Y);
            }

            frontClouds.Update(gameTime);
            backClouds.Update(gameTime);

            cloudTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (cloudTimer > cloudInterval)
            {
                cloudTimer -= cloudInterval;
                if (Rand.Next(4) == 0) backClouds.Add(new Cloud(sprites[Rand.Next(sprites.Count)]));
                else if (Rand.Next(4) == 0) frontClouds.Add(new Cloud(sprites[Rand.Next(sprites.Count)]));
            }
            
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(bg, Vector2.Zero, Color.White);
            backClouds.Draw(gameTime, spriteBatch);
            spriteBatch.Draw(title, Vector2.Zero, Color.White);
            frontClouds.Draw(gameTime, spriteBatch);
            
            start.Draw(spriteBatch);
            quit.Draw(spriteBatch);
        }
    }
}
